import get_field from '@dfeidao/atom-nodejs/data/get-field';
import complex_query from '@dfeidao/atom-nodejs/db/complex-query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { file_url, productid, systemid } from '../atom/config';
interface Message {
	productid: string;
	class_id: string;
	type: number; // 1 签到和未签到/2 请假
	sign_in?: number; // 1 签到/2 未签到
	page_size: number;
	page_no: number;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
interface D {
	headimg: {};
	student_no: string;
	sign_back: number;
}
export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:app-check-status/s001,action_id:' + action_id);
	const query = complex_query(action_id, session_id, systemid);
	if (message.type === 1) {// 未签到/已签到

		if (message.sign_in === 1) {// 未签到
			// --班级学生查学生和家长    未请假的
			// select s.student_no,s.student_name,s.sex,s.headimg,s.whether_enter_face,pi.phone from student s left join class_student cs on s.student_no=cs.student_no left join parent_information pi  on s.student_no=pi.student_no where cs.class_id='BJ000096' and pi.is_message_receiver='1' and cs.status!='2';
			const student = await query
				.add_field('student', 'student_no', 'student_no')
				.add_field('student', 'student_name', 'student_name')
				.add_field('student', 'sex', 'sex')
				.add_field('student', 'headimg', 'headimg')
				.add_field('student', 'whether_enter_face', 'whether_enter_face')
				.add_field('parent_information', 'phone', 'phone')
				.add_field('parent_information', 'parent_number', 'parent_number')
				.add_field('parent_information', 'parent_name', 'parent_name')
				.add_field('class_student', 'class_name', 'class_name')
				.add_field('class_student', 'class_id', 'class_id')
				.outer_left_join('student', 'class_student', ['student_no', 'student_no'])
				.outer_left_join('student', 'parent_information', ['student_no', 'student_no'])
				.where_eq('class_student', 'class_id', message.class_id)
				.where_eq('parent_information', 'is_message_receiver', '1')
				.where_neq('class_student', 'status', '1')
				.where_eq('student', 'productid', message.productid)
				.where_eq('class_student', 'productid', message.productid)
				.where_eq('parent_information', 'productid', message.productid)
				.exec() as D[];

			// 		- 班级学生查签到
			// select * from check_in where student_no in (select student_no from class_student where  class_id='BJ000096') and arrive_time>1557248400000
			const arrive_time = new Date();
			const student_no = await get_field(student, 'student_no');
			const check_in = await query
				.add_field('check_in', 'student_no', 'student_no')
				.where_in('check_in', 'student_no', student_no)
				.where_eq('check_in', 'productid', message.productid)
				.where_gte('check_in', 'arrive_time', arrive_time.setHours(0, 0, 0, 0))
				.where_lte('check_in', 'arrive_time', arrive_time.setHours(23, 59, 59, 999))
				.exec() as D[];
			const student_no_check_in = await get_field(check_in, 'student_no');
			const student_list = student.filter((v) => {
				if (student_no_check_in.indexOf(v.student_no) === -1) {
					v.headimg = v.headimg ? {
						uri: file_url + '/getfile?productid=' + productid + '&id=' + v.headimg
					} : '';
					return v;
				}
			});
			log('Service end 未签到 path:app-check-status/s001,action_id:' + action_id);
			// 少数据分页
			const student_list_1 = student_list.slice((message.page_no - 1) * message.page_size, message.page_no * message.page_size);
			return {
				data: student_list_1
			};
		} else {// 已签到
			// 班级学生查学生和家长,查签到
			// 			select s.student_no,s.student_name,s.sex,s.headimg,s.whether_enter_face,pi.phone,s.productid from student s
			// left join class_student cs on s.student_no=cs.student_no
			// left join parent_information pi  on s.student_no=pi.student_no
			// left join check_in ci  on ci.student_no=cs.student_no
			// where cs.class_id='BJ000096' and pi.is_message_receiver='1' and cs.status!='1' and s.productid='U0000026' and ci.arrive_time>1557227400000;
			const arrive_time = new Date();
			const student = await query
				.add_field('student', 'student_no', 'student_no')
				.add_field('student', 'student_name', 'student_name')
				.add_field('student', 'sex', 'sex')
				.add_field('student', 'headimg', 'headimg')
				.add_field('student', 'whether_enter_face', 'whether_enter_face')
				.add_field('parent_information', 'phone', 'phone')
				.add_field('parent_information', 'parent_number', 'parent_number')
				.add_field('parent_information', 'parent_name', 'parent_name')
				.add_field('class_student', 'class_name', 'class_name')
				.add_field('class_student', 'class_id', 'class_id')
				.outer_left_join('student', 'class_student', ['student_no', 'student_no'])
				.outer_left_join('student', 'parent_information', ['student_no', 'student_no'])
				.outer_left_join('class_student', 'check_in', ['student_no', 'student_no'])
				.where_eq('class_student', 'class_id', message.class_id)
				.where_eq('parent_information', 'is_message_receiver', '1')
				.where_neq('class_student', 'status', '1')
				.where_eq('student', 'productid', message.productid)
				.where_eq('class_student', 'productid', message.productid)
				.where_eq('parent_information', 'productid', message.productid)
				.where_eq('check_in', 'productid', message.productid)
				.where_gte('check_in', 'arrive_time', arrive_time.setHours(0, 0, 0, 0))
				.where_lte('check_in', 'arrive_time', arrive_time.setHours(23, 59, 59, 999))
				.page(message.page_size, message.page_no)
				.exec() as D[];
			// 班级
			const student_no = await get_field(student, 'student_no');
			const leave_time = new Date();
			const sign_off = await query
				.add_field('sign_off', 'student_no', 'student_no')
				.where_in('sign_off', 'student_no', student_no)
				.where_eq('sign_off', 'productid', message.productid)
				.where_gte('sign_off', 'leave_time', leave_time.setHours(0, 0, 0, 0))
				.where_lte('sign_off', 'leave_time', leave_time.setHours(23, 59, 59, 999))
				.exec() as D[];
			const student_no_sign_off = await get_field(sign_off, 'student_no');
			const student_list = student.filter((v) => {
				v.headimg = v.headimg ? {
					uri: file_url + '/getfile?productid=' + productid + '&id=' + v.headimg
				} : '';
				if (student_no_sign_off.indexOf(v.student_no) === -1) {
					v.sign_back = 1;
				} else {
					v.sign_back = 0;
				}
				return v;
			});
			log('Service end 已签到 path:app-check-status/s001,action_id:' + action_id);
			// for (let i = 0; i < 15; i++) {
			// 	student_list.push(student_list[0]);
			// }
			return {
				data: student_list
			};
		}

	} else { // 请假
		// --班级学生查学生和家长    请假的
		// select s.student_no,s.student_name,s.sex,s.headimg,s.whether_enter_face,pi.phone from student s left join class_student cs on s.student_no=cs.student_no left join parent_information pi  on s.student_no=pi.student_no where cs.class_id='BJ000096' and pi.is_message_receiver='1' and cs.status='1';
		const student = await query
			.add_field('student', 'student_no', 'student_no')
			.add_field('student', 'student_name', 'student_name')
			.add_field('student', 'sex', 'sex')
			.add_field('student', 'headimg', 'headimg')
			.add_field('student', 'whether_enter_face', 'whether_enter_face')
			.add_field('parent_information', 'phone', 'phone')
			.add_field('class_student', 'class_name', 'class_name')
			.outer_left_join('student', 'class_student', ['student_no', 'student_no'])
			.outer_left_join('student', 'parent_information', ['student_no', 'student_no'])
			.where_eq('class_student', 'class_id', message.class_id)
			.where_eq('parent_information', 'is_message_receiver', '1')
			.where_eq('class_student', 'status', '1')
			.where_eq('student', 'productid', message.productid)
			.where_eq('class_student', 'productid', message.productid)
			.where_eq('parent_information', 'productid', message.productid)
			.exec() as D[];
		log('Service end 请假 path:app-check-status/s001,action_id:' + action_id);
		student.forEach((v) => {
			v.headimg = v.headimg ? {
				uri: file_url + '/getfile?productid=' + productid + '&id=' + v.headimg
			} : '';
		});
		return {
			data: student
		};
	}
}
